package com.onepiece.gen.admin.config;

import com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationAdvisor;
import com.baomidou.dynamic.datasource.processor.DsProcessor;
import com.onepiece.gen.frame.aop.DynamicDatasourcePackageInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.aop.Advisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Administrator
 * Date 2022/11/28 16:45
 * Version 1.0
 * Description TODO
 **/
@Slf4j
@Configuration
@MapperScan(basePackages = {
        "com.onepiece.gen.admin.*.mapper"
})
public class MybatisPlusConfig {

    @Bean
    public Advisor dynamicDatasourcePackageAnnotationAdvisor(DsProcessor dsProcessor) {
        DynamicDatasourcePackageInterceptor interceptor = new DynamicDatasourcePackageInterceptor(dsProcessor);
        Map<String, String> patternMap = new HashMap<>();
        patternMap.put("com.onepiece.gen.admin.rcp", "rcp");
        patternMap.put("com.onepiece.gen.admin.rdp", "rdp");
        interceptor.addPatternMap(patternMap);
        DynamicDataSourceAnnotationAdvisor advisor = new DynamicDataSourceAnnotationAdvisor(interceptor, Service.class);
        return advisor;
    }
}
